package com.example.videodemo.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.jna.HCNetSDKJNAInstance;
import com.example.videodemo.DemoViewModel;
import com.example.videodemo.DeviceItem;
import com.example.videodemo.R;
import com.example.videodemo.RcvData;
import com.example.videodemo.RcvDemoAdapter;
import com.example.videodemo.RcvJpg;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.INT_PTR;
import com.hikvision.netsdk.NET_DVR_DEVICECFG_V40;

import java.util.ArrayList;
import java.util.List;

import static com.hikvision.netsdk.HCNetSDK.NET_DVR_GET_DEVICECFG_V40;


public class MainFragment extends Fragment {
    private DemoViewModel demoViewModel;
    private RecyclerView recyclerView;
    private TextView textView;
    private int m_lUserID;
    private DeviceItem deviceItem;
    private List<RcvJpg> list;
    private int iPreviewID;
    public MainFragment() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        demoViewModel = new ViewModelProvider(this.getActivity()).get(DemoViewModel.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_main, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rcv_main);
        textView = (TextView) view.findViewById(R.id.txt_DVRType);
        RcvData data=new RcvData("北门", "admin", "Pc@12138", "192.168.7.30", "8000");
        RcvData sfc1 = new RcvData("测试", "admin", "Hik12345", "192.168.7.53", "8000");
        demoViewModel.setRcvData(sfc1);
        deviceItem = demoViewModel.login();
        m_lUserID = deviceItem.m_lUserID;
        list=new ArrayList<>();
        list=demoViewModel.getRcvChanel();
        int byDVRType = deviceItem.m_struDeviceInfoV40_jna.struDeviceV30.wDevType;
        NET_DVR_DEVICECFG_V40 devicecfg_v40 =new NET_DVR_DEVICECFG_V40();
        Boolean f= HCNetSDK.getInstance().NET_DVR_GetDVRConfig(deviceItem.m_lUserID,NET_DVR_GET_DEVICECFG_V40,0xFFFFFFFF,devicecfg_v40);
        if (f){
            byte[] typeName = devicecfg_v40.byDevTypeName;
            String s=new String(typeName);
            textView.setText("设备类型:"+s);
        }else {
            Log.e("++++",HCNetSDK.getInstance().NET_DVR_GetErrorMsg(new INT_PTR()));

            textView.setText("设备类型:"+byDVRType);
        }


        RcvDemoAdapter adapter = new RcvDemoAdapter(R.layout.rcv_item,list);



        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle =new Bundle();
                bundle.putInt("position",position);
                Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_playFragment,bundle);

            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        recyclerView.setAdapter(adapter);

        return view;
    }



}
